<template>
  <view>
   <view class="header">
     <view class="back" @click="back()">
       <uni-icons type="left" size="20"></uni-icons>
     </view>
     <text>收货地址管理</text>
   </view>
    <view class="noAddress" v-if='addressInfo.length==0 '>
      <view class="image">
        <image src="../../static/noAddress.png" mode=""></image>
      </view>
      <view class="text">
        <view class="title">
          暂无收获地址
        </view>
        <view class="detail">
          您还没有收获地址，快来添加一个吧！
        </view>
      </view>
      <view class="addAddress" @click="goAddAddress()">
        立即新增
      </view>
    </view>
    <view class="addressInfo" v-else>
      <uni-swipe-action>
        <block v-for="(item,index) in addressInfo" :key="index">
          <uni-swipe-action-item :right-options="options" @click="del(item.addressId)">
            <view class="address-box">
              <view class="mainInfo">
                <text>{{item.recipientName}} {{item.phoneNumber}}</text>
                <view class="isDefault" v-if="item.isDefault">
                  默认
                </view>
              </view>
              <view class="addressDetail">
                {{item.privince+item.city+item.county+item.detailAddress}}
              </view>
              <view class="edit" @click="goAddAddress(item)">
                编辑
              </view>
            </view>
          </uni-swipe-action-item>
        </block>
      </uni-swipe-action>
    </view>

    <view class="footer">
      <view class="addAddress" @click="goAddAddress()">
        新增收货地址
      </view>
    </view>
  </view>
</template>

<script>
  import {
    mapState
  } from 'vuex'
  export default {
    data() {
      return {
        addressInfo: [],
        options: [{
          text: '删除', // 显示的文本内容
          style: {
            backgroundColor: '#f00' // 按钮的背景颜色
          }
        }]
      };
    },
    onLoad() {
      this.getAddressInfo()
    },
    onShow() {
      this.getAddressInfo()
    },
    methods: {
      back() {
        uni.navigateBack()
      },
      goAddAddress(item = 0) {
        uni.navigateTo({
          url: '/subpkg/addAddress/addAddress?item=' + encodeURIComponent(JSON.stringify(item))
        })
      },
      async getAddressInfo() {
        const {
          data: {
            data
          }
        } = await uni.$http.get(`/users/address/queryAllAddress/${this.userInfo.studentId}`)
        this.addressInfo = data
      },
      async del(id) {
        await uni.$http.delete(`/users/address/delete/address?userId=${this.userInfo.studentId}&addressId=${id}`)
      }
    },
    computed: {
      ...mapState('my_user', ['userInfo'])
    }
  }
</script>

<style lang="scss">
  page {
    background-color: #fff;
  }
  
  .header {
    position: relative;
    width: 100%;
    height: 140rpx;
    text-align: center;
    font-size: 36rpx;
    font-weight: 700;
    line-height: 170rpx;
    background-color: #fff;
  
    .back {
      position: absolute;
      top: 4rpx;
      left: 23rpx;
      height: 50rpx;
    }
  }

  .noAddress {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding-top: 350rpx;

    .image {
      width: 450rpx;
      height: 275rpx;

      image {
        width: 100%;
        height: 100%;
      }
    }

    .text {
      margin-top: 30rpx;

      .title {
        text-align: center;
        font-size: 40rpx;
        color: #555;
      }

      .detail {
        margin-top: 10rpx;
        font-size: 30rpx;
        color: #aaa;
      }
    }

    .addAddress {
      margin-top: 30rpx;
      border: 1rpx solid #b6eaae;
      border-radius: 10rpx;
      padding: 20rpx 30rpx;
      color: #b6eaae;
      font-size: 32rpx;
    }
  }

  .addressInfo {
    display: flex;
    flex-direction: column;
    padding-left: 40rpx;
    border-top: 1rpx solid #eee;

    .address-box {
      position: relative;
      border-bottom: 1rpx solid #eee;
      padding: 30rpx 0;

      .mainInfo {
        display: flex;
        align-items: center;
        height: 60rpx;
        font-size: 34rpx;
        color: #555;

        text {
          font-weight: 700;
        }

        .isDefault {
          margin-left: 10rpx;
          font-size: 20rpx;
          padding: 8rpx 15rpx;
          border-radius: 6rpx;
          background-color: #b6eaae;
          color: #fff;
        }
      }

      .addressDetail {
        font-size: 28rpx;
        margin-top: 40rpx;
        color: #999;
      }

      .edit {
        position: absolute;
        top: 64rpx;
        right: 20rpx;
        border: 1rpx solid #eee;
        border-radius: 6rpx;
        padding: 10rpx 20rpx;
        font-size: 28rpx;
        color: #666;
      }
    }
  }

  .footer {
    position: fixed;
    bottom: 0;
    width: 100%;
    height: 130rpx;
    padding: 20rpx 30rpx;
    border-top: 1rpx solid #e9e9e9;

    .addAddress {
      width: 90%;
      height: 90rpx;
      border-radius: 6rpx;
      text-align: center;
      line-height: 90rpx;
      background-color: #b6eaae;
      color: #fff;
      font-size: 38rpx;
    }
  }
</style>